﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class GiangVienController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /KhachHang/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.GiangViens
                                select h;
            return View();
        }

        //
        // GET: /KhachHang/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /KhachHang/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {            
            return View();
        }

        //
        // POST: /KhachHang/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(GiangVien giangvien)
        {
            try
            {
                // TODO: Add insert logic here
              
                db.GiangViens.InsertOnSubmit(giangvien);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {                
                return View();
            }
        }

        //
        // GET: /KhachHang/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            GiangVien model = (from m in db.GiangViens
                               where m.id == id
                               select m).SingleOrDefault();            
            return View(model);
        }

        //
        // POST: /KhachHang/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, GiangVien giangvien)
        {
            GiangVien model = (from m in db.GiangViens
                               where m.id == id
                               select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.tengv = giangvien.tengv;
                model.ngsinh = giangvien.ngsinh;
                model.diachi = giangvien.diachi;
                model.dienthoai = giangvien.dienthoai;
                model.email = giangvien.email;
                model.bangcap = giangvien.bangcap;
                model.chuyennganh = giangvien.chuyennganh;
                model.ghichu = giangvien.ghichu;
                
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {                
                return View(model);
            }
        }

        //
        // GET: /KhachHang/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.GiangViens
                         where h.id == id
                         select h).Take(1).SingleOrDefault();
            db.GiangViens.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /KhachHang/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
